package com.lcxw.hxzbadmin.controller.goodnamecontroller;

import com.alibaba.fastjson.JSONObject;
import com.lcxw.hxzbcommon.entity.ErrorCode;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbadmin.entity.pojo.GoodName;
import com.lcxw.hxzbadmin.service.baseservice.GoodNameService;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbcommon.common.StringUtil;

@RestController
@CrossOrigin
@RequestMapping("admin/goodName")
public class GoodNameController {

    private Logger logger = LoggerFactory.getLogger(GoodNameController.class);

    @Resource
    GoodNameService goodNameService;

    @PostMapping("/add")
    public Response addGoodName(@RequestBody JSONObject requestParams) {
        logger.info("admin {} add a good name", requestParams.getString("adminName"));

        String name = requestParams.getString("name");
        try {
            if (null != goodNameService.selectByName(name)) {
                return Response.common(logger, "select good name already exist.", ErrorCode.INVALID_REQ,
                    "the name is already exist.", null);
            }
        } catch (Exception e) {
            return Response.dbFail(logger, "select good name");
        }

        GoodName goodName = new GoodName();

        goodName.setName(name);
        goodName.setPrice(requestParams.getInteger("price"));
        goodName.setSold(0);
        goodName.setAddTime(StringUtil.getDateTime());

        try {
            goodNameService.insert(goodName);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.dbFail(logger, "add good name");
        }

        return Response.common(logger, "add a good name success.", 0, "success", null);

    }



}
